﻿//2013-12-10
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using PSS.Entity;
using PSS.Entity.SetManage.BasicData;
using PSS.Utility;

namespace PSS.DAL.SetManage.BasicData
{
    public class StorageDAL : DbHelperBase
    {
        public int GetTotal()
        {
            var strSql = new StringBuilder();
            strSql.Append(" select  count(*) AS [Total] FROM PSS_Storage ");
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["Total"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo:" + ex.Message);
                }
                return count;
            }
        }
        public IList<StorageEntity> GetStorageList(Pagination qcmodel)
        {
            var list = new List<StorageEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            var start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            var end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by CreateTime) AS idx,StorageId,StorageCode,StorageName,Status,Remark,CreateTime FROM PSS_Storage ");
            strSql.Append(" WHERE 1=1 ");
            strSql.Append(") AS T ");
            strSql.Append(" Where T.idx between " + start + " and " + end);
            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new StorageEntity();
                            if (reader["StorageId"] != DBNull.Value)
                                model.StorageId = new Guid(reader["StorageId"].ToString());
                            model.StorageCode = reader["StorageCode"] == obj ? null : Convert.ToString(reader["StorageCode"]);
                            model.StorageName = reader["StorageName"] == obj ? null : Convert.ToString(reader["StorageName"]);
                            if (reader["Status"] != DBNull.Value)
                                model.Status = (bool)reader["Status"];
                            model.Remark = reader["Remark"] == obj ? null : Convert.ToString(reader["Remark"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        public IList<StorageEntity> GetStorageListByCombobox()
        {
            var list = new List<StorageEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT StorageId,StorageName FROM PSS_Storage where Status=1 ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new StorageEntity();
                            if (reader["StorageId"] != DBNull.Value)
                                model.StorageId = new Guid(reader["StorageId"].ToString());
                            model.StorageName = reader["StorageName"] == obj ? null : Convert.ToString(reader["StorageName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        public bool ModifyStorage(StorageEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE [PSS_Storage] SET [StorageCode]=@StorageCode,[StorageName] = @StorageName,[Status]=@Status,[Remark]=@Remark WHERE [StorageId] = @StorageId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@StorageCode", model.StorageCode);
                    Db.AddInParameter(cmd, "@StorageName", model.StorageName);
                    Db.AddInParameter(cmd, "@Status", model.Status);
                    Db.AddInParameter(cmd, "@Remark", model.Remark);
                    Db.AddInParameter(cmd, "@StorageId", model.StorageId);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        public bool AddStorage(StorageEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" INSERT INTO [PSS_Storage]([StorageId],[StorageCode],[StorageName],[Status],[Remark]) ");
            strSql.Append(" VALUES(@StorageId,@StorageCode,@StorageName,@Status,@Remark) ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@StorageId", model.StorageId);
                    Db.AddInParameter(cmd, "@StorageCode", model.StorageCode);
                    Db.AddInParameter(cmd, "@StorageName", model.StorageName);
                    Db.AddInParameter(cmd, "@Status", model.Status);
                    Db.AddInParameter(cmd, "@Remark", model.Remark);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        public StorageEntity GetStorageInfo(string id)
        {
            var model = new StorageEntity();
            object obj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT [StorageId],[StorageCode],[StorageName],[Status],[Remark] FROM [PSS_Storage] where [StorageId]=@StorageId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {

                try
                {
                    Db.AddInParameter(cmd, "@StorageId", id);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["StorageId"] != DBNull.Value)
                                model.StorageId = new Guid(reader["StorageId"].ToString());
                            model.StorageCode = reader["StorageCode"] == obj ? null : Convert.ToString(reader["StorageCode"]);
                            model.StorageName = reader["StorageName"] == obj ? null : Convert.ToString(reader["StorageName"]);
                            if (reader["Status"] != DBNull.Value)
                                model.Status = (bool)reader["Status"];
                            model.Remark = reader["Remark"] == obj ? null : Convert.ToString(reader["Remark"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }
        public bool RemoveStorage(string id)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" DELETE FROM [PSS_Storage] WHERE StorageId=@StorageId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@StorageId", id);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
    }
}
